#include "sd_flash.h"
#include "ui_sd_flash.h"
#include "control.h"
#include <QDebug>

sd_flash::sd_flash(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::sd_flash)
{
    ui->setupUi(this);

        setWindowFlag(Qt::FramelessWindowHint);
}

sd_flash::~sd_flash()
{
    delete ui;
}

void sd_flash::sdflash_play()
{

    char flash_data[50] ={0};
    int rx_length=0;
    int index=0;
    char * buff_p = NULL;

    extern QSerialPort *serial;

    if(serial->isOpen()==true)
    {
        extern  QByteArray getcmd;
        rx_length=getcmd.length()-4;
        buff_p = NULL;
        buff_p = strstr(getcmd,"DATA");
    }

    extern int tcp_state;
    if(tcp_state)
    {
        extern QByteArray TCP_buff;
        qDebug() << TCP_buff <<endl;
        rx_length=TCP_buff.length()-4;
        buff_p = NULL;
        buff_p = strstr(TCP_buff,"DATA");
    }

    if(buff_p != NULL)
    {
       buff_p+=4;
       flash_data[rx_length] = '\0';
 //      qDebug() << getcmd <<endl;
       while(rx_length--)
       {
          if(*buff_p == '@')
          {

              break;
          }
          else
          {
             flash_data[index] = *buff_p;

          }
        buff_p++;
        index++;
       }
       ui->SDREAD->setText(flash_data);
       ui->FLASHREAD->setText(flash_data);
    }





}

void sd_flash::on_sd_read_clicked()
{
    extern QSerialPort *serial;

    if(serial->isOpen()==true)
    {
        serial->write("@ADD");
        serial->write(ui->SD_address->text().toUtf8());
        serial->write("_");
        serial->write("SIZE");
        serial->write(ui->SD_size->text().toUtf8());
        serial->write("_");
        serial->write("READ .");
    }

    extern int tcp_state;
    if(tcp_state)
    {
        extern QTcpSocket *qtcpsocket;
        qtcpsocket->write("@ADD");
        qtcpsocket->write(ui->SD_address->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("SIZE");
        qtcpsocket->write(ui->SD_size->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("READ .");
    }


}

void sd_flash::on_sd_write_clicked()
{
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        serial->write("@");
        serial->write("ADD");
        serial->write(ui->SD_address->text().toUtf8());
        serial->write("_");
        serial->write("SIZE");
        serial->write(ui->SD_size->text().toUtf8());
        serial->write("_");
        serial->write("WRITE");
        serial->write(ui->SDWRITE->text().toUtf8());
        serial->write(" ");
        serial->write(".");

    }
    extern int tcp_state;
    if(tcp_state)
    {
        extern QTcpSocket *qtcpsocket;
        qtcpsocket->write("@");
        qtcpsocket->write("ADD");
        qtcpsocket->write(ui->SD_address->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("SIZE");
        qtcpsocket->write(ui->SD_size->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("WRITE");
        qtcpsocket->write(ui->SDWRITE->text().toUtf8());
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }




}

void sd_flash::on_flash_read_clicked()
{
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
      serial->write("@");
      serial->write("FLASH_R");
      serial->write(ui->sector->currentText().toUtf8());
      serial->write(" ");
      serial->write(".");
    }



    extern int tcp_state;
    if(tcp_state)
    {
        extern QTcpSocket *qtcpsocket;
        qtcpsocket->write("@");
        qtcpsocket->write("FLASH_R");
        qtcpsocket->write(ui->sector->currentText().toUtf8());
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }



}



void sd_flash::on_flash_write_clicked()
{
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        serial->write("@");
        serial->write("FLASH_W");
        serial->write(ui->sector->currentText().toUtf8());
        serial->write(":");
        serial->write(ui->FLASHWRITE->text().toUtf8());
        serial->write(" ");
        serial->write(".");
    }

    extern int tcp_state;
    if(tcp_state)
    {
        extern QTcpSocket *qtcpsocket;
        qtcpsocket->write("@");
        qtcpsocket->write("FLASH_W");
        qtcpsocket->write(ui->sector->currentText().toUtf8());
        qtcpsocket->write(":");
        qtcpsocket->write(ui->FLASHWRITE->text().toUtf8());
        qtcpsocket->write(" ");
        qtcpsocket->write(".");



    }
}

void sd_flash::on_pushButton_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;

    extern int tcp_state;
    int i;
       for(i=0;i<5;i++)
       {

           if(serial->isOpen()==true)
           {
               serial->write("@");
               serial->write("close");
               serial->write(" ");
               serial->write(".");
           }

       }  if(tcp_state == 1)
       {
           qtcpsocket->write("@");
           qtcpsocket->write("close");
           qtcpsocket->write(" ");
           qtcpsocket->write(".");
       }
       this->close();
}

void sd_flash::on_FLASH_SELECT_clicked()
{

}
